﻿using Microsoft.Extensions.Logging;
using System;
using UtilZ.Dotnet.Ex.ZLog;


namespace UtilZ.Dotnet.Extensions.Log
{
    /// <summary>
    /// 
    /// </summary>
    public class RefZLoger : ILogger, IDisposable
    {
        private static RefZLoger _instance;
        /// <summary>
        /// 
        /// </summary>
        public static RefZLoger Instance
        {
            get { return _instance; }
        }
        static RefZLoger()
        {
            _instance = new RefZLoger(); 
        }



        /// <summary>
        /// 
        /// </summary>
        public RefZLoger()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TState"></typeparam>
        /// <param name="state"></param>
        /// <returns></returns>
        public IDisposable BeginScope<TState>(TState state)
        {
            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logLevel"></param>
        /// <returns></returns>
        public bool IsEnabled(LogLevel logLevel)
        {
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TState"></typeparam>
        /// <param name="logLevel"></param>
        /// <param name="eventId"></param>
        /// <param name="state"></param>
        /// <param name="exception"></param>
        /// <param name="formatter"></param>
        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
        {
            try
            {
                string format;
                if (formatter != null)
                {
                    format = formatter(state, exception);
                }
                else
                {
                    if (state != null)
                    {
                        format = state.ToString();
                    }
                    else
                    {
                        format = null;
                    }
                }

                switch (logLevel)
                {
                    case LogLevel.Trace:
                        ZLoger.Trace(eventId.Id, null, exception, format);
                        break;
                    case LogLevel.Debug:
                        ZLoger.Debug(eventId.Id, null, exception, format);
                        break;
                    case LogLevel.Information:
                        ZLoger.Info(eventId.Id, null, exception, format);
                        break;
                    case LogLevel.Warning:
                        ZLoger.Warn(eventId.Id, null, exception, format);
                        break;
                    case LogLevel.Error:
                        ZLoger.Error(eventId.Id, null, exception, format);
                        break;
                    case LogLevel.Critical:
                        ZLoger.Fatal(eventId.Id, null, exception, format);
                        break;
                    case LogLevel.None:
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                ZLoger.Error(ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {

        }
    }
}
